/** 
 * @Title: Carry.java 
 * @Package participle.carry 
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月26日 下午9:16:27 
 */
package participle.carry;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;

/**
 * @ClassName: Carry
 * @Description: TODO 分词
 * @author HeYan
 * @date 2016年9月26日 下午9:16:27
 * 
 */
public class Carry implements Runnable {
	private String table;
	private BufferedWriter buf;

	/**
	 * @param table
	 * @throws IOException
	 */
	public Carry(String table) throws IOException {
		super();
		this.table = table;
		File file = new File("/var/server/java/heyan/word/" + table + ".txt");
		if (!file.exists()) {
			file.createNewFile();
		}
		this.buf = new BufferedWriter(new FileWriter(file, true));
	}

	public void getFileContent() throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(new File("/var/server/java/heyan/file10/" + table)));
		String temp = "";
		while ((temp = br.readLine()) != null) {
			carry(temp);
		}
		br.close();
	}

	public void carry(String flag) throws IOException {
		List<Term> list = HanLP.segment(flag);
		for (Term t : list) {
			if (!t.nature.equals("w")) {
				buf.write(t.word + "\t");
			}
		}
		buf.write("\n");
	}

	public void run() {
		try {
			getFileContent();
			System.out.println(table);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				buf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws IOException {
		ExecutorService fix = Executors.newFixedThreadPool(50);
		String path = "/var/server/java/heyan/file10";
		File[] files = new File(path).listFiles();
		for (File file : files) {
			String name = file.getName();
			fix.execute(new Carry(name));
		}
		fix.shutdown();
	}
}
